<?php
session_start();

require_once("../config/include_tokyo.inc.php");

//访问权限验证

$userRole = $_SESSION['user_role'];
if($userRole == 1 ||  $userRole == 5){
	//管理员,地址管理员,任务管理员+地址管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}



//更新地址内容
$doFlag = $_POST["doFlag"];
if($doFlag == "updateAddress"){
	$addressId = $_POST["addressId"];
	$_GET['id'] = $addressId;
	$factory_name = $_POST["factory_name"];
	$factory_address = $_POST["factory_address"];
	$use_flag = $_POST["use_flag"];
	
	$sql = "update factoryaddress set factory_name = '".$factory_name."'";
	$sql = $sql.", factory_address='".$factory_address."'";
	$sql = $sql.", use_flag=".$use_flag;
	$sql = $sql."  where id=".$addressId;	
	$db->Execute($sql);

	
}else if($doFlag == "insertAddress"){
	//保存 并返回到列表
	$factory_name = $_POST["factory_name"];
	$factory_address = $_POST["factory_address"];
	$use_flag = $_POST["use_flag"];

	
	$sql = "INSERT INTO factoryaddress
		(factory_name,factory_address,use_flag) 
		values 
  		('$factory_name','$factory_address',$use_flag)";
	$db->Execute($sql);
	
}else if($doFlag == "deleteAddress"){
	$addressId = $_POST["addressId"];
	$sql = "delete from factoryaddress where id=".$addressId;
	$db->Execute($sql);
}

//地址详情 
$id = $_GET['id'];
if($id == ""){
	//新增地址
}else{
	//编辑地址
	$sql = "SELECT * from factoryaddress where id=".$id;
	$result = $db->Execute($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "地址详情"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="inputForm" action="../factory/addressInput.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>地址详情:
<?php  if($doFlag == "updateAddress"){echo "地址内容更新成功!";}?>
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
if($id == ""){
	$strTR = "";
	$strTR = $strTR."<tr ><td>工厂名称</td>";
	$strTR = $strTR."<td><input type='text' name='factory_name' id='factory_name'></td></tr>";
	$strTR = $strTR."<tr ><td>工厂地址</td>";
	$strTR = $strTR."<td><input type='text' name='factory_address' id='factory_address' size=100></td></tr>";
	$strTR = $strTR."<tr ><td>是否在用</td>";
	$strTR = $strTR."<td><input type='text' name='use_flag' id='use_flag' value=0><br>0=能用，1=不能用(不良地址)</td></tr>";
	$strTR = $strTR."<tr><td colspan=2 align=center>";
	$strTR = $strTR."<input type='button' value='新增' onclick='insertAddress()'>";
	$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
	$strTR = $strTR."</td></tr>";
	echo $strTR;
	
}else{	
	if(!$result->EOF){
		$strTR = "";
		$strTR = $strTR."<tr ><td>ID</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>工厂名称</td>";
		$strTR = $strTR."<td><input type='text' name='factory_name' id='factory_name' value='".$result->fields['factory_name']."'></td></tr>";
		$strTR = $strTR."<tr ><td>工厂地址</td>";
		$strTR = $strTR."<td><input type='text' name='factory_address' id='factory_address' value='".$result->fields['factory_address']."' size=100></td></tr>";

		$strTR = $strTR."<tr ><td>是否在用</td>";
		$strTR = $strTR."<td><input type='text' name='use_flag' id='use_flag' value='".$result->fields['use_flag']."'><br>0=能用，1=不能用(不良地址)</td></tr>";
		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='修改' onclick='updateAddress(".$id.")'>";
		$strTR = $strTR."<input type='button' value='删除' onclick='deleteAddress(".$id.")'>";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
		
	}
}
?>

</table>



</div>

<br><br>
<br><br>

</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="addressId" id="addressId" >
</form>
</body>




</html>

<script>
<?php
if($doFlag == "insertAddress"){
	echo "window.location.href = 'addressList.php'";
}
?>

function updateAddress(id){
	document.getElementById('doFlag').value = 'updateAddress';
	document.getElementById('addressId').value = id;
	document.inputForm.submit();
}

function insertAddress(){
	document.getElementById('doFlag').value = 'insertAddress';	
	document.inputForm.submit();
}

function toList(){
	window.location.href = "addressList.php";
}

function deleteAddress(id){
	if(confirm("确认要删除？")){
		document.getElementById('doFlag').value = 'deleteAddress';
		document.getElementById('addressId').value = id;
		document.inputForm.submit();
	}
}
</script>

